﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VNWIKI_WEB;
using VNWIKI_WEB.Models;

namespace VNWIKI_WEB.Controllers
{ 
    public class UserController : Controller
    {
        VNWIKIDataContext db = new VNWIKIDataContext();

        //
        // GET: /User/

        public ActionResult Index()
        {
            User admin = (User)Session["user"];
            if (admin == null || admin.Admin == false)
                return RedirectToAction("LogOn", "Admin");

            return View(db.Users.ToList());
        }

        //
        // GET: /User/Details/5

        public ActionResult Details(int id)
        {
            User admin = (User)Session["user"];
            if (admin == null || admin.Admin == false)
                return RedirectToAction("LogOn", "Admin");

            User user = db.Users.Single(u=>u.ID == id);
            return View(user);
        }

        //
        // GET: /User/Create

        public ActionResult Create()
        {
            User admin = (User)Session["user"];
            if (admin == null || admin.Admin == false)
                return RedirectToAction("LogOn", "Admin");

            return View();
        } 

        //
        // POST: /User/Create

        [HttpPost]
        public ActionResult Create(User user)
        {
            if (ModelState.IsValid)
            {
                db.Users.InsertOnSubmit(user);
                db.SubmitChanges();
                return RedirectToAction("Index");  
            }

            return View(user);
        }
        
        //
        // GET: /User/Edit/5
 
        public ActionResult Edit(int id)
        {
            User admin = (User)Session["user"];
            if (admin == null || admin.Admin == false)
                return RedirectToAction("LogOn", "Admin");

            User user = db.Users.Single(u => u.ID == id);
            return View(user);
        }

        //
        // POST: /User/Edit/5

        [HttpPost]
        public ActionResult Edit(User user)
        {
            if (ModelState.IsValid)
            {
                User user1 = db.Users.Single(u => u.ID == user.ID);
                user1.Name = user.Name;
                user1.AccountName = user.AccountName;
                user1.CreateDate = user.CreateDate;
                user1.Deleted = user.Deleted;
                user1.DeleteDate = user.DeleteDate;
                user1.Email = user.Email;
                user1.Password = user.Password;
                user1.Permision = user.Permision;
                user1.Admin = user.Admin;
                db.SubmitChanges();
                return RedirectToAction("Index");
            }
            return View(user);
        }

        //
        // GET: /User/Delete/5
 
        public ActionResult Delete(int id)
        {
            User admin = (User)Session["user"];
            if (admin == null || admin.Admin == false)
                return RedirectToAction("LogOn", "Admin");

            User user = db.Users.Single(u => u.ID == id);
            return View(user);
        }

        //
        // POST: /User/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            User user = db.Users.Single(u => u.ID == id);
            db.Users.DeleteOnSubmit(user);
            db.SubmitChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}